package com.ustc.emailcheck;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class Account implements Serializable{	
	
	private static final long serialVersionUID = -8441333691744849585L;
	
	public static final String ACCOUNT_FILE="account.dat";
	
	private String name,serverType,server,username,password,emailclient;
	private int count=0;
	
	public Account() {
		super();
	}

	
	public Account(String name, String serverType, String server,
			String username, String password,String emailclient) {
		super();
		this.name = name;
		this.serverType = serverType;
		this.server = server;
		this.username = username;
		this.password = password;
		this.emailclient = emailclient;
	}


	public Account(String name, String serverType, String server,
			String username, String password, int count) {
		super();
		this.name = name;
		this.serverType = serverType;
		this.server = server;
		this.username = username;
		this.password = password;
		this.count = count;
	}


	public String getEmailclient() {
		return emailclient;
	}


	public void setEmailclient(String emailclient) {
		this.emailclient = emailclient;
	}


	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}


	public String getServerType() {
		return serverType;
	}


	public void setServerType(String serverType) {
		this.serverType = serverType;
	}


	public String getServer() {
		return server;
	}


	public void setServer(String server) {
		this.server = server;
	}


	public String getUsername() {
		return username;
	}


	public void setUsername(String username) {
		this.username = username;
	}


	public String getPassword() {
		return password;
	}


	public void setPassword(String password) {
		this.password = password;
	}


	public int getCount() {
		return count;
	}


	public void setCount(int count) {
		this.count = count;
	}


	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + count;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result
				+ ((password == null) ? 0 : password.hashCode());
		result = prime * result + ((server == null) ? 0 : server.hashCode());
		result = prime * result
				+ ((serverType == null) ? 0 : serverType.hashCode());
		result = prime * result
				+ ((username == null) ? 0 : username.hashCode());
		return result;
	}


	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final Account other = (Account) obj;
		if (count != other.count)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (password == null) {
			if (other.password != null)
				return false;
		} else if (!password.equals(other.password))
			return false;
		if (server == null) {
			if (other.server != null)
				return false;
		} else if (!server.equals(other.server))
			return false;
		if (serverType == null) {
			if (other.serverType != null)
				return false;
		} else if (!serverType.equals(other.serverType))
			return false;
		if (username == null) {
			if (other.username != null)
				return false;
		} else if (!username.equals(other.username))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "{name="+name+",serverType="+serverType+",server="+server +
				",username="+username+",password="+password+"}";
	}
	
	public static void saveAccount(Account[] accounts){
		try {
			FileOutputStream fos=new FileOutputStream(ACCOUNT_FILE);
			ObjectOutputStream oos=new ObjectOutputStream(fos);
			oos.writeObject(accounts);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static Account[] loadAccount(){
		Account[] accounts=null;
		try {
			FileInputStream fis=new FileInputStream(ACCOUNT_FILE);
			ObjectInputStream ois=new ObjectInputStream(fis);
			accounts=(Account[])ois.readObject();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return accounts;
	}
}
